package com.schoolai.ai.component;

import com.schoolai.entity.SchoolAiDietaryRequirements;
import com.schoolai.feign.IFeignSchoolDietaryRequirementsontroller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Copyright(C),2019-2025，XX公司
 * FileName:Vecto
 * Author:VectorStoreConfig
 * 创建时间：2025/9/26 09:39
 * Description:存储向量数据
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 */
@Slf4j
@Component
public class VectorStoreConfig {
    /// 嵌入式模型
    @Autowired
    EmbeddingModel embeddingModel;

    @Autowired
    IFeignSchoolDietaryRequirementsontroller iFeignSchoolAiDietaryRequirementsController;

    @Bean("CustomVectorStore")
    VectorStore vectorStore(){
        log.info("加载向量数据");
        System.out.println("加载向量数据");
        /// 1、读取午餐要求内容
        List<SchoolAiDietaryRequirements> SchoolAiDietaryRequirementsList = iFeignSchoolAiDietaryRequirementsController.list();

        StringBuffer recommendationBuff = new StringBuffer("乌当区实验小学餐饮要求：");
        SchoolAiDietaryRequirementsList.forEach(e->{
            recommendationBuff.append(e.getContent()).append("\n");
        });
        /// 2、使用嵌入模型转化向量
        VectorStore vectorStore = SimpleVectorStore.builder(embeddingModel).build();
        Document document1 = Document.builder()
                .text(recommendationBuff.toString()).build();
        //存储向量(内部会自动向量化)
        vectorStore.add(List.of(document1));
        return vectorStore;
    }
}
